package foodmaterial.action;

import foodmaterial.entity.JsonResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

public class BindingResultUtil {

    public static JsonResult<String> error(BindingResult bindingResult) {
        JsonResult<String> jsonResult = new JsonResult<>() ;
        jsonResult.setSuccess(true);
        if (bindingResult.hasErrors()) {
            ObjectError objectError = bindingResult.getAllErrors().get(0) ;
            if (objectError instanceof FieldError) {
                FieldError fieldError = (FieldError)objectError ;
                jsonResult.setSuccess(false);
                jsonResult.setErrorMsg(fieldError.getDefaultMessage());
            }
        }
        return jsonResult ;
    }
}
